//
//  DetailViewController.m
//  Cat
//
//  Created by lushitong on 2018/12/23.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailTableView.h"
#import "HttpHomeManager.h"
#import "ShareViewController.h"
#import "CouponInfo.h"
#import "LoginViewController.h"
#import "QKShareLiveSheetManager.h"
#import "TaobaoAuthorView.h"

@interface DetailViewController ()

@property (nonatomic, strong) DetailTableView *tableView;
@property (nonatomic, strong) UIView *operView;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UIButton *getCouponButton;
@property (nonatomic, strong) ProductInfo *infoModel;
@property (nonatomic, strong) TaobaoAuthorView *anthorView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	self.topTitleView.backgroundColor = [UIColor clearColor];
	self.topTitleView.bottomLineView.hidden = YES;
	[self loadAllSubViews];
	
	[HttpHomeManager getProductId:self.proudctID withSuccess:^(ProductInfo *ProductModel) {
		self.infoModel = ProductModel;
		[self.getCouponButton setTitle:[NSString stringWithFormat:@"领券 ￥%@",ProductModel.couponAmount]];
		self.tableView.productID = self.proudctID;
		[self.tableView setModel:ProductModel];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)getCouponAction {
	[HttpHomeManager getShareCouponWithProductId:self.proudctID withSuccess:^(CouponInfo *couponModel) {
		[self showItemInTaobao4iOS:couponModel];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
        if (code == 1004) {
            [HttpHomeManager authGetinfoSuccess:^(NSString *authString) {
                [self pushStfari:authString];
            } failure:^(NSInteger code, NSString *message) {
                [FLProgressHUD showError:message];
            }];
            return;
        }
		[FLProgressHUD showError:message];
	}];
}

- (void)showItemInTaobao4iOS:(CouponInfo *)info {
	// 构建淘宝客户端协议的 URL
	

	NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"taobao:%@",info.url]];
	// 判断当前系统是否有安装淘宝客户端
	if ([[UIApplication sharedApplication] canOpenURL:url] && info.url.length > 0) {
		// 如果已经安装淘宝客户端，就使用客户端打开链接
		[[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
			
		}];
		
	} else {
		NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"taobao://item.taobao.com/item.htm?id=%@", info.productId]];
		if ([[UIApplication sharedApplication] canOpenURL:url]) {
			[[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
				
			}];
		} else {
			url = [NSURL URLWithString:[NSString stringWithFormat:@"http://item.taobao.com/item.htm?id=%@", info.productId]];
			[[UIApplication sharedApplication] openURL:url];
		}
	}
	
}

- (void)shareButtonAction {
	[HttpHomeManager getShareInfoWithProductId:self.proudctID withSuccess:^(ShareCouponInfo *shareCouponModel) {
		ShareViewController *shareVC = [[ShareViewController  alloc] init];
		shareVC.productModel = self.infoModel;
		shareVC.model = shareCouponModel;
		[self.navigationController pushViewController:shareVC animated:YES];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		if (code == -1002) {
			LoginViewController *login = [[LoginViewController alloc] init];
			[self.navigationController pushViewController:login animated:YES];
			return;
        } else if (code == 1004) {
            [HttpHomeManager authGetinfoSuccess:^(NSString *authString) {
                [self pushStfari:authString];
            } failure:^(NSInteger code, NSString *message) {
                [FLProgressHUD showError:message];
            }];
            return;
        }
		[FLProgressHUD showError:message];
	}];
}


- (void)pushStfari:(NSString *)url {
    
    [QKShareLiveSheetManager showAnthorSuperView:nil customView:self.anthorView];
    [self.anthorView pushAnthoreBlock:^{
        NSURL *openUlr = [NSURL URLWithString:url];
        
        if ([[UIApplication sharedApplication] canOpenURL:openUlr]) {
            [[UIApplication sharedApplication] openURL:openUlr options:nil completionHandler:^(BOOL success) {
                
            }];
        } else {
            [[UIApplication sharedApplication] openURL:openUlr];
        }
    }];
}

- (void)loadAllSubViews {
	[self.view addSubview:self.operView];
	[self.view addSubview:self.tableView];
	[self.view bringSubviewToFront:self.topTitleView];
	
	[self.operView addSubviews:@[self.shareButton,self.getCouponButton]];
	
	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.and.bottom.equalTo(self.view);
		make.height.equalTo(@(kQKFitModuleDecimal(50.0)));
	}];
	
	[self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left);
		make.bottom.equalTo(self.view.mas_bottom);
		make.width.equalTo(@(kQKFitModuleDecimal(132)));
		make.height.equalTo(@(kQKFitModuleDecimal(50.0)));
	}];
	
	[self.getCouponButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.shareButton.mas_right);
		make.right.and.bottom.equalTo(self.view);
		make.height.equalTo(@(kQKFitModuleDecimal(50.0)));
	}];
}

#pragma mark----- lazy init -----

- (UIView *)operView {
	if (!_operView) {
		_operView = [[UIView alloc] init];
	}
	return _operView;
}

- (UIButton *)shareButton {
	if (!_shareButton) {
		_shareButton = [UIButton buttonWithTitle:@"分享" color:COLOR_WITH_HEX(0xFFFFFF) font:[UIFont systemFontOfSize:kQKFitFont(16)] cornerRadius:0.0];
		_shareButton.backgroundColor = COLOR_WITH_HEX(0xFFC236);
		[_shareButton addTarget:self action:@selector(shareButtonAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _shareButton;
}

- (UIButton *)getCouponButton {
	if (!_getCouponButton) {
		_getCouponButton = [UIButton buttonWithTitle:@"" color:COLOR_WITH_HEX(0xFFFFFF) font:[UIFont systemFontOfSize:kQKFitFont(16)] cornerRadius:0.0];
		_getCouponButton.backgroundColor = COLOR_WITH_HEX(0xFE5F53);
		[_getCouponButton addTarget:self action:@selector(getCouponAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _getCouponButton;
		
}

- (DetailTableView *)tableView {
	if (!_tableView) {
		_tableView = [[DetailTableView alloc] initWithFrame:CGRectMake(0.0, 0.0, ScreenWidth, ScreenHeight - kQKFitModuleDecimal(50.0)) sections:4];
	}
	return _tableView;
}

- (TaobaoAuthorView *)anthorView {
    if (!_anthorView) {
        _anthorView = [[TaobaoAuthorView alloc] initWithFrame:CGRectMake(0.0, 0.0, ScreenWidth - 2 *kQKFitModuleDecimal(40.0), kQKFitModuleDecimal(240))];
    }
    return _anthorView;
}
@end

